HTTP Server

  • A basic HTTP server. Currently this is implemented using the PoCSocket abstraction, but the intention is to remove this dependency and reimplement the class using transport APIs provided by the Server APIs working group.

    See more

    Declaration

    Swift

    public class HTTPServer: HTTPServing
  • Definition of an HTTP server.

    See more

    Declaration

    Swift

    public protocol HTTPServing : class
  • Typealias for a closure that handles an incoming HTTP request The following is an example of an echo HTTPRequestHandler that returns the request it receives as a response:

       func echo(request: HTTPRequest, response: HTTPResponseWriter ) -> HTTPBodyProcessing {
           response.writeHeader(status: .ok)
           return .processBody { (chunk, stop) in
               switch chunk {
               case .chunk(let data, let finishedProcessing):
                   response.writeBody(data) { _ in
                       finishedProcessing()
                   }
               case .end:
                   response.done()
               default:
                   stop = true
                   response.abort()
               }
           }
       }
    

    This then needs to be registered with the server using HTTPServer.start(port:handler:)

    Declaration

    Swift

    public typealias HTTPRequestHandler = (HTTPRequest, HTTPResponseWriter) -> HTTPBodyProcessing

    Parameters

    req

    the incoming HTTP request.

    res

    a writer providing functions to create an HTTP reponse to the request.

  • Class protocol containing a handle() function that implements HTTPRequestHandler to respond to incoming HTTP requests.

    See

    HTTPRequestHandler for more information
    See more

    Declaration

    Swift

    public protocol HTTPRequestHandling: class